var token = window.localStorage.getItem('token')

$(function () {
  var copy = new Clipboard('#Copy')
  $('#Copy').tooltip({ trigger: "manual", placement: "right", title: "Copied!" });


  copy.on('success', function (e) {
    $('#Copy')
      .tooltip('show')
      .mouseout(function (e) {
        $(this)
          .tooltip('hide')
          .mouseout(null)
      })
    e.clearSelection();
  });


  if (token) {
    $('#Token').val(token)
  }
  else {
    console.error('错误！token是空的')
  }
})

$('#tietuku').change(function (e) {
  if ($('#tietuku-pic').val() === '') {
    return false;
  }

  var formData = new FormData($('form')[0]);

  $.ajax({
    url: 'http://up.imgapi.com/',
    type: 'POST',
    xhr: function () {
      myXhr = $.ajaxSettings.xhr();
      if (myXhr.upload) {
        myXhr.upload.addEventListener('progress', progressHandlingFunction, false);
      }
      return myXhr;
    },
    beforeSend: function () {
      $('.progress-bar').css({ "width": 0 });
      $('.fake').addClass('fake-off')
      $('.loading').show();
    },
    success: function (data) {
      $('.loading').hide();
      $('.fake').removeClass('fake-off')
      $('.fake').css({ 'background-image': 'url(' + data.t_url + ')' })
      $('#picture-link').val(data.ubburl)
    },
    error: function (data) {
      console.log(data);
    },
    data: formData,
    cache: false,
    contentType: false,
    processData: false
  });
})

function progressHandlingFunction(e) {
  if (e.lengthComputable) {
    var loaded = parseFloat(e.loaded)
    var total = parseFloat(e.total)

    if (isNaN(loaded) || isNaN(total)) {
      return "-";
    }
    var value = total <= 0 ? "0%" : (Math.round(loaded / total * 10000) / 100.00 + "%");
    $('.progress-bar').css({ "width": value });
  }
}

$('#token-setting').click(function () {
  var token = prompt('请输入你的Tokne,Token获取方式：http://www.tietuku.com/manager/createtoken')
  localStorage.setItem('token', token)
  $('#Token').val(token)
})